// pages/view/view.js
//获取应用实例
const app = getApp()
var html2wxml = require('../../html2wxml/html2wxml.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aid: '',
    title: '',
    imageUrl: '',
    content: [],
    phoneNumber: '',
    me: '',
    address: '',
    email: '',
    scan: false,
    canIUse: wx.canIUse('official-account')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    })
    const scene = decodeURIComponent(options.scene)
    console.log('scene', scene)
    if (scene != 'undefined') {
      this.fetchViewData(scene);
      this.setData({
        aid: scene,
        scan: true
      });
      var self = this;
      wx.request({
        url: "https://m.afwscl.com/xcx/image.php?aid=" + scene,
        success: function (res) {
          console.log('拉取封面图成功', res)
          self.setData({
            imageUrl: res.data.litpic
          })
        },
        fail: function (res) {
          console.log('拉取封面图失败', res)
        }
      })
    } else {
      this.fetchViewData(options.aid);
      this.setData({
        aid: options.aid
      });
      var self = this;
      wx.request({
        url: "https://m.afwscl.com/xcx/image.php?aid=" + options.aid,
        success: function (res) {
          console.log('拉取封面图成功', res)
          self.setData({
            imageUrl: res.data.litpic
          })
        },
        fail: function (res) {
          console.log('拉取封面图失败', res)
        }
      })
    }
    wx.request({
      url: "https://wx.ooooooooooo.cn/afxcx/xcx/contact.php",
      success: function (res) {
        console.log('拉取联系信息成功', res)
        self.setData({
          phoneNumber: res.data.phoneNumber,
          me: res.data.me,
          address: res.data.address,
          email: res.data.email
        })
      },
      fail: function (res) {
        console.log('拉取联系信息失败', res)
      }
    })
  },
  //给a标签添加跳转和复制链接事件
  wxmlTagATap: function (e) {
    var self = this;
    var href = e.currentTarget.dataset.src;
    console.log(href);
    wx.setClipboardData({
      data: href,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '链接已复制',
              //icon: 'success',
              image: '../../image/link.png',
              duration: 2000
            })
          }
        })
      }
    })
  },

  //获取文章内容
  fetchViewData: function (aid) {
    var self = this;
    wx.request({
      url: "https://m.afwscl.com/xcx/view.php?aid=" + aid,
      success: function (res) {
        console.log('拉取文章成功', res)
        var post = res.data
        var title = post.title
        self.setData({
          title: title
        })
        html2wxml.html2wxml('body', post.body, self, 5)
        setTimeout(function () {
          wx.hideLoading()
        }, 300)
        wx.setNavigationBarTitle({
          title: res.data.title,
          success: function (res) {
            // success
          }
        });
      },
      fail: function (res) {
        console.log('拉取文章失败', res)
      }
    })
  },
  makePhoneCall: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.phoneNumber
    })
  },
  backIndex: function () {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  markerTap: function (e) {
    wx.openLocation({
      latitude: 36.053232,
      longitude: 119.393910,
      scale: 18,
      name: '诸城市奥丰环保设备有限公司',
      address: '山东省潍坊市诸城市206国道与舜苑路交叉路口西500米'
    })
  },
  copyEmailTap: function (e) {
    wx.setClipboardData({
      data: this.data.email,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '邮箱已复制',
              icon: 'success',
              duration: 2000
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  onPageScroll: function (e) {
    // Do something when page scroll
    //console.log(e)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: this.data.title,
      path: '/pages/view/view?aid=' + this.data.aid,
      imageUrl: this.data.imageUrl,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})